package collection;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import javax.swing.JCheckBox;

public class ArrayListEx extends JFrame {

	private JPanel contentPane;
	private JTextField inputData;
	private JButton btnNewButton;
	private JButton btnNewButton_1;
	private JButton btnNewButton_2;
	private JButton btnNewButton_3;
	private JScrollPane scrollPane;
	private JTextArea result;
	private JButton btnNewButton_4;

	ArrayList list = new ArrayList();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ArrayListEx frame = new ArrayListEx();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	int pos = -1; // 검색된 데이터의 위치
	String str = ""; // 수정된 문자열 저장
	private JCheckBox cNC;
	private JPanel panel;

	/**
	 * Create the frame.
	 */
	public ArrayListEx() {
		setTitle("ArrayList Example");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		contentPane.add(getInputData());
		contentPane.add(getBtnNewButton());
		contentPane.add(getScrollPane());
		contentPane.add(getBtnNewButton_4());
		contentPane.add(getPanel());
		contentPane.add(getBtnNewButton_3());
	}

	private JTextField getInputData() {
		if (inputData == null) {
			inputData = new JTextField();
			inputData.setBounds(12, 13, 246, 38);
			inputData.setColumns(10);
		}
		return inputData;
	}

	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("추가");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					// if(!"".equals(inputData.getText())){ //입력창과 공백이 같지 않다
					// list.add(inputData.getText());
					// result.append("\n저장되었습니다");
					// inputData.setText("");
					// result.append("데이터가"+list.size()+"건 출력되었습니다");
					// inputData.requestFocus();
					// }else if("".equals(inputData.getText())){
					// JOptionPane.showMessageDialog(null,"데이터가 입력되었습니다");
					// }
					String temp = inputData.getText();
					if (temp.equals("")) {
						JOptionPane.showMessageDialog(null,
								"공백 문자는 저장되지 않습니다.!!!");
					} else {
						list.add(temp);
						inputData.setText("");
						inputData.requestFocus();

						result.append("\n저장되었습니다");
						inputData.setText("");
						result.append("데이터가" + list.size() + "건 출력되었습니다");

					}

				}
			});
			btnNewButton.setBounds(12, 61, 67, 23);
		}
		return btnNewButton;
	}

	private JButton getBtnNewButton_1() {
		if (btnNewButton_1 == null) {
			btnNewButton_1 = new JButton("삭제");
			btnNewButton_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					boolean b = cNC.isSelected();
					int cnt = 0;

					if (pos >= 0) {
						if (b) {
							for (int i = list.size() - 1; i >= 0; i--) {
								if (list.get(i).equals(inputData.getText())) {
									list.remove(i);
									cnt++;
								}
							}
							result.append("\n" + str + "이(가)" + cnt
									+ " 삭제되었습니다.");
						} else {
							list.remove(pos);
							result.append("\n" + str + "이(가) 삭제되었습니다.");
						}
						btnNewButton_1.setEnabled(false);
						btnNewButton_2.setEnabled(false);
					}
				}
			});
			btnNewButton_1.setEnabled(false);
		}
		return btnNewButton_1;
	}

	private JButton getBtnNewButton_2() {
		if (btnNewButton_2 == null) {
			btnNewButton_2 = new JButton("수정");
			btnNewButton_2.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					boolean b = cNC.isSelected();
					
					if (pos >= 0) {
						if (b) {
							for (int i = 0; i < list.size(); i++) {
								if (list.get(i).equals(str)) {
									list.set(i, inputData.getText());
								}
							}
						} else {
							list.set(pos, inputData.getText());
						}

						btnNewButton_1.setEnabled(false);
						btnNewButton_2.setEnabled(false);
						result.append("\n" + str + "이" + inputData.getText()
								+ "으(로) 수정되었습니다.");
					}
				}
			});
			btnNewButton_2.setEnabled(false);
		}
		return btnNewButton_2;
	}

	private JButton getBtnNewButton_3() {
		if (btnNewButton_3 == null) {
			btnNewButton_3 = new JButton("출력");
			btnNewButton_3.setBounds(91, 61, 67, 23);
			btnNewButton_3.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					int length = list.size();
					for (int i = 0; i < list.size(); i++) {
						result.append("\n" + list.get(i));
					}

				}
			});
		}
		return btnNewButton_3;
	}

	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBounds(12, 115, 361, 137);
			scrollPane.setViewportView(getResult());
		}
		return scrollPane;
	}

	private JTextArea getResult() {
		if (result == null) {
			result = new JTextArea();
		}
		return result;
	}

	private JButton getBtnNewButton_4() {
		if (btnNewButton_4 == null) {
			btnNewButton_4 = new JButton("조회");
			btnNewButton_4.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					pos = -1;
					str = "";
					for (int i = 0; i < list.size(); i++) {
						if (list.get(i).equals(inputData.getText())) {
							pos = i;
							str = (String) list.get(i);
							result.append("\n찾았습니다");

							btnNewButton_1.setEnabled(true);
							btnNewButton_2.setEnabled(true);
							break;
						}
					}
					if (pos == -1) {
						btnNewButton_1.setEnabled(false);
						btnNewButton_2.setEnabled(false);
						JOptionPane.showMessageDialog(null, "데이터가 없습니다");
					}
				}
			});
			btnNewButton_4.setBounds(170, 61, 70, 23);
		}
		return btnNewButton_4;
	}

	private JCheckBox getCNC() {
		if (cNC == null) {
			cNC = new JCheckBox("New check box");
			cNC.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {

					JFrame jFrame = new JFrame("Selecting CheckBox");
					jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

				}
			});
		}
		return cNC;
	}

	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setBounds(278, 13, 144, 63);
			panel.add(getCNC());
			panel.add(getBtnNewButton_2());
			panel.add(getBtnNewButton_1());
		}
		return panel;
	}
}
